/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.matezk.dms.controller.things;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.matezk.dms.things.model.entity.NotifyConfig;
import cn.matezk.dms.things.model.query.DeviceQuery;
import cn.matezk.dms.things.model.query.EventQuery;
import cn.matezk.dms.things.model.req.DeviceConfigReq;
import cn.matezk.dms.things.model.req.DeviceReq;
import cn.matezk.dms.things.model.resp.*;
import cn.matezk.dms.things.service.DeviceService;
import cn.matezk.dms.things.service.EventService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;

import java.util.List;

/**
 * 设备管理 API
 *
 * @author 程道林
 * @since 2024/12/02 10:59
 */
@Tag(name = "设备管理 API")
@Validated
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/things/device", api = {Api.PAGE, Api.DETAIL, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class DeviceController extends BaseController<DeviceService, DeviceResp, DeviceDetailResp, DeviceQuery, DeviceReq> {

    private final DeviceService deviceService;
    private final EventService eventService;

    @Operation(summary = "设备参数配置", description = "设备参数配置")
    @SaCheckPermission("things:device:config")
    @PostMapping("config")
    void config(@RequestBody DeviceConfigReq configReq) {
        deviceService.config(configReq);
    }

    @Operation(summary = "通知配置", description = "通知配置")
    @PostMapping("notifyConfig/{id}")
    void notifyConfig(@PathVariable Long id, @RequestBody NotifyConfig notifyConfig) {
        deviceService.notifyConfig(id, notifyConfig);
    }

    @Operation(summary = "设备事件", description = "设备事件")
    @GetMapping("event")
    PageResp<EventResp> event(EventQuery eventQuery, @Validated PageQuery pageQuery) {
        return eventService.page(eventQuery, pageQuery);
    }

    @Operation(summary = "设备属性", description = "设备属性")
    @GetMapping("property")
    List<PropertyResp> property(@RequestParam List<String> deviceIds) {
        return deviceService.property(deviceIds);
    }

    @Operation(summary = "销售统计", description = "销售统计")
    @GetMapping("saleStat")
    SaleStatResp saleStat() {
        return deviceService.saleStat();
    }

    @Operation(summary = "销售排行", description = "销售排行")
    @GetMapping("hotSales")
    HotSalesResp hotSales() {
        return deviceService.hotSales();
    }
}